<?php
namespace AuctionCore\Other;

use AuctionCore\Model\AuCarOwner;

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: luchao@clcw.com.cn                                             |
// +------------------------------------------------------------------------+
//


/**
 * 抽奖活动列表
 * @author       luchao
 */
class RaffleList
{

    /**
     * 获取活动列表
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     * @return multitype:array(list,total)
     */
    public function getList($page = 1, $size = 10, $paramer)
    {
        $map = array();
        //开始时间
        if ($paramer['start_time'] && !$paramer['end_time']) {
            $map['starttime'] = array('egt', $paramer['start_time']." 00:00:00");
        }elseif ($paramer['end_time'] && !$paramer['start_time']) {
            $map['starttime'] = array('elt', $paramer['end_time']." 23:59:59");
        }elseif ($paramer['start_time'] && $paramer['end_time']) {
            $map['starttime'] = array(array('egt', $paramer['start_time']." 00:00:00"),array('elt', $paramer['end_time']." 23:59:59"));
        }

        //关键字
        if ($paramer['keyword']) {
            $where = array();
            $where['activity_no'] = array('eq', $paramer['keyword']);
            $where['a_name'] = array('like', '%' . $paramer['keyword'] . '%');
            $where['amount'] = array('eq', $paramer['keyword']);
            $where['_logic'] = 'OR';
            $map['_complex'] = $where;
        }
        $prefix = C('DB_PREFIX');
        $total = M('activity')->where($map)->count();
        $list = M('activity')->where($map)->limit($size)->page($page)->order("aid desc")->select();
        // echo  M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    //奖券列表 未发放 已过期
    public function not_gave_card_list($page = 1, $size = 10, $params){
        $map = array();
        $nowdate = date("Y-m-d H:i:s");
        $map['c.a_id'] = array('eq',$params['aid']);
        $map['c.order_id'] = array('eq',0);
        if ($params['status'] == 1) {
            $map['a.endtime'] = array('egt',$nowdate);
        }elseif ($params['status'] == 3) {
            $map['a.endtime'] = array('lt',$nowdate);
        }
        //关键字
        if ($params['search_key']) {
            $where = array();
            $where['c.card_no'] = array('eq', $params['search_key']);
            $where['c.price'] = array('eq', $params['search_key']);
            $where['_logic'] = 'OR';
            $map['_complex'] = $where;
        }
        $prefix = C('DB_PREFIX');
        $cols = 'c.card_no,c.price,c.createtime,a.starttime,a.endtime';
        $total = M('scratch_card')->alias('c')->join( $prefix."activity a ON c.a_id = a.aid")
            ->where($map)->count();
        $list = M('scratch_card')->alias('c')->field($cols)->join( $prefix."activity a ON c.a_id = a.aid")
            ->where($map)->limit($size)->page($page)->order("c.createtime desc")->select();
            // echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    // 奖券列表  --- 已发放
    public function gave_card_list($page = 1, $size = 10, $params){
        $map = array();
        $map['c.a_id'] = array('eq',$params['aid']);
        $map['c.order_id'] = array('neq',0);
        if ($params['state'] !== '') {
            $map['c.state'] = array('eq',$params['state']);
        }
        if($params['use_state'] !== ''){
            $map['c.use_state'] = array('eq',$params['use_state']);
        }
        //开始时间
        if ($params['start_time'] && !$params['end_time']) {
            $map['pay.verify_time'] = array('egt', $params['start_time']." 00:00:00");
        }elseif ($params['end_time'] && !$params['start_time']) {
            $map['pay.verify_time'] = array('elt', $params['end_time']." 23:59:59");
        }elseif ($params['start_time'] && $params['end_time']) {
            $map['pay.verify_time'] = array(array('egt', $params['start_time']." 00:00:00"),array('elt', $params['end_time']." 23:59:59"));
        }
        //关键字
        if ($params['search_key']) {
            $where = array();
            $where['c.card_no'] = array('eq', $params['search_key']);
            $where['o.order_no'] = array('eq', $params['search_key']);
            $where['cd.contact_person'] = array('like', "%".$params['search_key']."%");
            $where['_logic'] = 'OR';
            $map['_complex'] = $where;
        }
        $prefix = C('DB_PREFIX');
        $cols = 'c.card_no,c.state,c.use_state,c.createtime,c.price,o.order_no,cd.contact_person,cars.brand_id,cars.series_id,cars.model_id,pay.verify_time';
        $total = M('scratch_card')->alias('c')
            ->join($prefix."activity a  ON c.a_id = a.aid")
            ->join($prefix."order o ON o.order_id= c.order_id")
            ->join($prefix."car_dealer as cd ON cd.dealer_id = o.success_dealer_id")
            ->join($prefix."cars as cars ON cars.car_id = o.car_id")
            ->join("LEFT JOIN " . $prefix."proceeds_log pay ON o.order_id = pay.order_id")
            ->where($map)->count();
        $list = M('scratch_card')->alias('c')->field($cols)
            ->join($prefix."activity a  ON c.a_id = a.aid")
            ->join($prefix."order o ON o.order_id= c.order_id")
            ->join($prefix."car_dealer as cd ON cd.dealer_id = o.success_dealer_id")
            ->join($prefix."cars as cars ON cars.car_id = o.car_id")
            ->join("LEFT JOIN " . $prefix."proceeds_log pay ON o.order_id = pay.order_id")
            ->where($map)->limit($size)->page($page)->order("c.createtime desc")->select();
            // echo M()->getLastSql();
            // die();
            //获取车型
            $cm = new \AuctionCore\CarResource\CarModel();
            foreach ($list as $key => $value) {
                $brand = $cm->get_model_by_id($value['brand_id'], 'brand');
                $series = $cm->get_model_by_id($value['series_id'], 'series');
                $model = $cm->get_model_by_id($value['model_id']);
                $list[$key]['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
                //state 0未抽奖 ，1为已抽奖
                //use_state 0未使用，1为已使用
                if ($value['state'] == "0" && $value['use_state'] == "0") {
                    $list[$key]['guaka_state'] = "未抽奖";
                    $list[$key]['shiyong_state'] = "--";
                }elseif ($value['state'] == "1" && $value['use_state'] == "0") {
                    $list[$key]['guaka_state'] = "已抽奖";
                    $list[$key]['shiyong_state'] = "未使用";
                }elseif ($value['state'] == "1" && $value['use_state'] == "1") {
                    $list[$key]['guaka_state'] = "已抽奖";
                    $list[$key]['shiyong_state'] = "已使用";
                }
            }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }


    
}